<?php
class LoginModel extends Model
{
    function handleSubmit()
    {
        if(isset($_POST['submit']))
        {
            if(!is_null($_POST['id']) && !is_null($_POST['password']))
            {
                $this->loadTable('users');
                $condition = array(
                    'id' => $_POST['id'],
                    'password' => $_POST['password']
                );
                $rs = $this->tables['users']->get($condition);

                if($rs->getSize() > 0)
                {
                    $row = $rs->getRow(0);
                    $session = Session::getInstance();
                    $session->set('id',$row->id);
                    $session->set('permission', $row->permission);
                    $session->set('name',$row->name);
                    if($session->isExist('ref'))
                    {
                        Utility::redirect($session->get('ref'));
                    }
                    else
                        Utility::redirect(null);
                }
                else
                {
                    Utility::error("sai password !",'login');
                }

            }
        }
        else
        {
            Utility::redirect(NULL);
        }
    }
}

